package biz

import (
	"github.com/gin-gonic/gin"
	"lab.com/conf"
	"lab.com/consts"
	"lab.com/gen/entity"
	"lab.com/request"
)

type bannerBiz struct {
}

// Banner  轮播图
var Banner = bannerBiz{}

// List 轮播图列表
func (ach *bannerBiz) List(c *gin.Context) (out request.BannerListRes, err error) {
	ctx := conf.Span(c, "biz banner list", nil)
	bannerModel := conf.LabGen.Banner

	info, err := bannerModel.WithContext(ctx).Where(bannerModel.Status.Eq(consts.BannerNormal),
		bannerModel.IsShow.Eq(consts.BannerShow)).Order(bannerModel.Sort.Desc()).Find()
	if err != nil {
		return out, err
	}

	list := make([]request.BannerBasicInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.BannerBasicInfo{
			Id:       val.ID,
			SkipLink: val.SkipLink,
			ImgPath:  val.ImgPath,
		})
	}
	out.List = list
	out.Count = len(info)
	return out, nil
}

// Add 轮播图新增
func (ach *bannerBiz) Add(c *gin.Context, req request.BannerAddReq) (err error) {
	ctx := conf.Span(c, "biz banner add", nil)
	bannerModel := conf.LabGen.Banner

	info := &entity.Banner{
		Title:    req.Title,
		IsShow:   req.IsShow,
		ImgPath:  req.ImgPath,
		SkipLink: req.SkipLink,
		Sort:     req.Sort,
		Status:   consts.BannerNormal,
	}

	err = bannerModel.WithContext(ctx).Create(info)
	return nil
}

// List 轮播图列表
func (banner *bannerBiz) ListInManage(c *gin.Context, req request.BannerListInManageReq) (out request.BannerListInManageRes, err error) {
	ctx := conf.Span(c, "biz banner list in manage", nil)
	bannerModel := conf.LabGen.Banner

	info, count, err := bannerModel.WithContext(ctx).Where(bannerModel.Status.Eq(consts.BannerNormal)).Order(bannerModel.Sort.Desc()).FindByPage(req.Offset, req.Size)
	if err != nil {
		return out, err
	}

	list := make([]request.BannerInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.BannerInfo{
			Id:       val.ID,
			Title:    val.Title,
			ImgPath:  val.ImgPath,
			IsShow:   val.IsShow,
			SkipLink: val.SkipLink,
			Sort:     val.Sort,
		})
	}
	out.List = list
	out.Count = int(count)
	return out, nil
}

// Edit 轮播图编辑
func (banner *bannerBiz) Edit(c *gin.Context, req request.BannerEditReq) (err error) {
	ctx := conf.Span(c, "biz banner edit", nil)
	bannerModel := conf.LabGen.Banner

	info := &entity.Banner{
		Title:    req.Title,
		IsShow:   req.IsShow,
		ImgPath:  req.ImgPath,
		SkipLink: req.SkipLink,
		Sort:     req.Sort,
		Status:   consts.BannerNormal,
	}

	_, err = bannerModel.WithContext(ctx).Select(bannerModel.Title, bannerModel.IsShow, bannerModel.ImgPath,
		bannerModel.SkipLink, bannerModel.Sort, bannerModel.Status).
		Where(bannerModel.ID.Eq(req.ID)).Updates(info)
	return nil
}

// Delete 轮播图编辑
func (banner *bannerBiz) Delete(c *gin.Context, req request.BannerDeleteReq) (err error) {
	ctx := conf.Span(c, "biz banner delete", nil)
	bannerModel := conf.LabGen.Banner
	info := &entity.Banner{
		Status: consts.CooDelete,
	}

	_, err = bannerModel.WithContext(ctx).Select(bannerModel.Status).Where(bannerModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Show 轮播图显示
func (banner *bannerBiz) Show(c *gin.Context, req request.BannerShowReq) (err error) {
	ctx := conf.Span(c, "biz banner show", nil)
	bannerModel := conf.LabGen.Banner
	info := &entity.Banner{
		IsShow: req.IsShow,
	}

	_, err = bannerModel.WithContext(ctx).Select(bannerModel.IsShow).Where(bannerModel.ID.Eq(req.ID)).Updates(info)
	return err
}
